import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// 打包 可视化面板
import { visualizer } from 'rollup-plugin-visualizer';

// cdn 引入
import cdn from 'vite-plugin-cdn-import'
// gzip 压缩
import viteCompression from 'vite-plugin-compression'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    visualizer({ open: false }),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    cdn({
      modules: [
        {
          name: "vue",
          var: "Vue",
          path: "https://cdn.bootcdn.net/ajax/libs/vue/3.4.21/vue.cjs.min.js"
        },
        {
          name: "element-plus",
          var: "ElementPlus",
          path: "https://cdn.bootcdn.net/ajax/libs/element-plus/2.7.3/index.full.js",
          css: "https://cdn.bootcdn.net/ajax/libs/element-plus/2.7.3/index.min.css"
        }
      ]
    }),
    viteCompression({
      filter: /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i, // 需要压缩的文件
      threshold: 1024, // 文件容量大于这个值进行压缩
      algorithm: 'gzip', // 压缩方式
      ext: 'gz', // 后缀名
      deleteOriginFile: true, // 压缩后是否删除压缩源文件
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:3000',
        changeOrigin: true,
        rewrite: url => url.replace(/^\/api/, '')
      }
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@import "@/assets/variables.scss";`,
      },
    },
  }
})
